$(function () {
    init_cusSale();
    $("body").on('click','[data-stopPropagation]',function (e) {e.stopPropagation();});
    /**意向等级**/
    $(".multiple-select2").select2({placeholder:"选择意向客户"});
    $(".multiple-select2").on("change", function(e) {
        var yixiang = $("#bpGroud").val();
        var yArray = new Array();
        $.each(yixiang,function (index,val) {yArray.push(val);});
        var conver = yArray.join(",");
        $("#bpgroupSelect").val(conver);
        $JuXue.refreshTable("bigEmpTable");
    });

    /**导入批次**/
    $.getJSON(ctx+"bigEpExport/getlimitBigRecords",{},function (r) {
        var recodsArray = new Set();
        $.each(r,function (index,value) {
            recodsArray.add(value.epBatch);
        });
        $("#bpBatch").autocomplete({
            source: Array.from(recodsArray),
            select: function( event, ui ) {
                $("#bpBatch").val(ui.item.label);
                $JuXue.refreshTable("bigEmpTable");
            }
        });
    });

    $(".multiple-select3").select2({placeholder:"选择是否分配"});
    $(".multiple-select3").on("change", function(e) {
        var aiDistriute = $("#aiDistriute").val();
        $("#aiDistriuteSelect").val(aiDistriute);
        $JuXue.refreshTable("bigEmpTable");
    });


    $(".multiple-select4").select2({placeholder:"选择地区"});
    $(".multiple-select4").on("change", function(e) {
        var aiZone = $("#aiZone").val();
        $("#aiZoneSelect").val(aiZone);
        $JuXue.refreshTable("bigEmpTable");
    });

    $("#advance-daterange span").html(moment().subtract("days",29).format("YYYY-MM-DD")+" - "+moment().format("YYYY-MM-DD"));
    $('#advance-daterange').daterangepicker({
         timePicker: true, //显示时间
         timePicker24Hour: true, //时间制
         timePickerSeconds: true, //时间显示到秒
         startDate: moment().hours(0).minutes(0).seconds(0), //设置开始日期
         endDate: moment(new Date()), //设置结束器日期
         maxDate: moment(new Date()), //设置最大日期
         "opens": "center",
         ranges: {
             '今天': [moment(), moment()],'昨天': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
             '上周': [moment().subtract(6, 'days'), moment()],
             '前30天': [moment().subtract(29, 'days'), moment()],
             '本月': [moment().startOf('month'), moment().endOf('month')],
             '上月': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
         },
        showWeekNumbers: true,
        locale: {
             format: "YYYY-MM-DD HH:mm:ss", //设置显示格式
                 applyLabel: '确定', //确定按钮文本
                 cancelLabel: '取消', //取消按钮文本
                 customRangeLabel: '自定义',
                 daysOfWeek: ['日', '一', '二', '三', '四', '五', '六'],
                 monthNames: ['一月', '二月', '三月', '四月', '五月', '六月',
                     '七月', '八月', '九月', '十月', '十一月', '十二月'
                 ],
                firstDay: 1
         },
    }).on('apply.daterangepicker', function(ev, picker) {
        var starttime = picker.startDate.format('YYYY-MM-DD');
        var endtime = picker.endDate.format('YYYY-MM-DD');
        $("#advance-daterange span").html(starttime+" - "+endtime);
        $("#starttime").val(starttime);
        $("#endtime").val(endtime);
        $JuXue.refreshTable("bigEmpTable");
    });
    /**大资源批次的输入**/
    $('#bpBatch').live('input propertychange', function() {
        $JuXue.refreshTable("bigEmpTable");
    });
    /**专员输入搜索**/
    $('#bpdirect').live('input propertychange', function() {
        $JuXue.refreshTable("bigEmpTable");
    });
});

/**初始化客户**/
function init_cusSale(){
    var settings = {
        url: ctx + "aiEpResoure/list",
        pageSize: 15,
        queryParams: function(params) {
            return {
                pageSize: params.limit,
                pageNum: params.offset / params.limit + 1,
                //筛选条件
                aiBatch:  $("#bpBatch").val(),//批次
                aiDirector: $("#bpdirect").val(),
                aiGroud  :$("#bpgroupSelect").val(), //意向等级
                aiResType:$("#bpResType").val(), //大资源类型
                starttime :$("#starttime").val(),     //考试时间
                endtime :$("#endtime").val(),         //结束时间
                isBig:$("#aiDistriuteSelect").val(), //是否分配
                aiZone:$("#aiZoneSelect").val() //选择地区
            };
        },
        columns: [{
            checkbox: true,
        }, {
            field: 'aiName',
            title: '客户名称',
            width: 150,
            clickToSelect:false,
            class:'text-center',
            formatter:function (value, row, index) {
                return "<a href='javascript:void(0)' onclick='viewAiEmpDetail(this)' data-id='"+row.aiId+"'>"+value+"</a>";
            }
        }, {
            field: 'aiMobile',
            title: '手机号'
        }, {
            field: 'aiGroud',
            title: '意向等级'
        }, {
            field: 'aiDistriute',
            title: '是否分配',
            formatter:function (value, row, index) {
                if(value==null||value==""){
                    return "否";
                }else{
                    return "是";
                }
            }
        }, {
            field: 'aiZone',
            title: '地区'
        }, {
            field: 'aiCreatetime',
            title: '创建时间',
            formatter:function (value, row, index) {
                return value.substring(0,10);
            }
        }, {
            field: 'aiDirector',
            title: '创建人'
        }],
        onClickCell:function(field, value, row, $element){
            if(field == "aiName"){
                return ;
            }else{
                $(".theme-panel").addClass("active");
                $("#selectTheme").show();
                var selected = $("#bigEmpTable").bootstrapTable('getSelections');
                var selected_length = selected.length;
                $("#sutomeSelect").text(selected_length+1);
            }
        },
        onUncheck:function (row,ele) {
            var selected = $("#bigEmpTable").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                $(".theme-panel").removeClass("active");
                $("#selectTheme").hide();
            }
            $("#sutomeSelect").text(selected_length+1);
        },
        onCheck:function (row,ele) {
            var selected = $("#bigEmpTable").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                $(".theme-panel").removeClass("active");
                $("#selectTheme").hide();
            }else{
                $(".theme-panel").addClass("active");
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onUncheck:function (rows) {
            var selected = $("#bigEmpTable").bootstrapTable('getSelections');
            var selected_length = selected.length;
            if (!selected_length) {
                $(".theme-panel").removeClass("active");
                $("#selectTheme").hide();
            }else{
                $(".theme-panel").addClass("active");
                $("#selectTheme").show();
            }
            $("#sutomeSelect").text(selected_length);
        },
        onCheckAll:function (rows) {
            $(".theme-panel").addClass("active");
            $("#selectTheme").show();
            $("#sutomeSelect").text(rows.length);
        },
        onUncheckAll:function (rows) {
            $(".theme-panel").removeClass("active");
            $("#selectTheme").hide();
        }
    };
    $JuXue.initTable('bigEmpTable', settings);
}
/*导入客户*/
function addAiCustomer(){
    $.post(ctx+"aiEpResoure/addCustomerDialog",{},function (r) {
        $("#myCustomer-modal").empty().append(r).modal('show').draggable();
    });
}

